/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package analex;

/**
 *
 * @author Pedrom
 */
public class FsmNumber {

    public FsmNumber() {
    }

    public boolean isNumber(String lexema) {
        int current_state = 0;
        int next_state = 0;
        int i = 0;        
        while (i < lexema.length()) {
            
            switch (current_state) {
                case 0: // Estado Final
                    if (lexema.charAt(i) == '-' || Character.isDigit(lexema.charAt(i))) {
                        next_state = 0;
                        break;
                    }
                    if (lexema.charAt(i) == '.') {
                        next_state = 1;
                        break;
                    } else {
                        return false;
                    }                    
                case 1:
                    if(Character.isDigit(lexema.charAt(i))){
                        next_state = 2;
                    } else {
                        return false;
                    }
                    break;
                case 2: // Estado Final
                    if(Character.isDigit(lexema.charAt(i))){
                        next_state = 2;
                    } else {
                        return false;
                    }
                    break;
            }
            i++;
            current_state = next_state;
        }         
        if(current_state == 0 || current_state == 2){ // Se acabar nos estados finais.
            return true;
        } else {
            return false;
        }
    }
}
